package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.Code;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.domain.CheckGroup;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.BusinessException;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查组业务实现类
 */
@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    /**
     * 添加检查组
     *
     * @param checkGroup
     * @param checkitemIds
     */
    @Transactional
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        if (ObjectUtils.isEmpty(checkGroup) || StringUtils.isEmpty(checkGroup.getCode()) || StringUtils.isEmpty(checkGroup.getName())) {
            throw new BusinessException(MessageConstant.CRITICAL_DATA_EMPTY, Code.DATA_VALUE_ERROR);
        }
        try {
            checkGroupDao.addCheckGroup(checkGroup);
        } catch (Exception e) {
            throw new BusinessException(MessageConstant.ADD_CHECKGROUP_FAIL, Code.SAVE_ERROR);
        }
        if (checkitemIds != null || checkitemIds.length > 0) {
            this.addCheckGroupAndCheckItem(checkGroup, checkitemIds);
        }
    }


    /**
     * 分页查询
     *
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        if (currentPage == null || currentPage < 1) {
            currentPage = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckGroup> groupList = null;
        try {
            groupList = checkGroupDao.selectByCondition(queryString);
        } catch (Exception e) {
            throw new BusinessException(MessageConstant.QUERY_CHECKGROUP_FAIL,Code.GET_ERROR);
        }
        long total = groupList.getTotal();
        List<CheckGroup> result = groupList.getResult();
        return new PageResult(total, result);
    }


    /**
     * 根据检查组ID查询关联的检查项ID
     *
     * @param id
     * @return
     */
    @Override
    public Integer[] findCheckItemIds(Integer id) {
        try {
            Integer[] checkItemById = checkGroupDao.findCheckItemById(id);
            return checkItemById;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(MessageConstant.QUERY_CHECKITEM_FAIL,Code.GET_ERROR);
        }
    }

    /**
     * 根据id查询检查组
     *
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        try {
            CheckGroup checkGroup = checkGroupDao.findCheckGroupById(id);
            return checkGroup;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(MessageConstant.QUERY_CHECKGROUP_FAIL,Code.GET_ERROR);
        }
    }

    /**
     * 修改检查组
     *
     * @param checkGroup
     * @param checkitemIds
     */
    @Transactional
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        if (ObjectUtils.isEmpty(checkGroup) || ObjectUtils.isEmpty(checkGroup.getId())) {
            throw new BusinessException(MessageConstant.CRITICAL_DATA_EMPTY,Code.DATA_VALUE_ERROR);
        }
        if (StringUtils.isEmpty(checkGroup.getCode()) || StringUtils.isEmpty(checkGroup.getName())) {
            throw new BusinessException(MessageConstant.CRITICAL_DATA_EMPTY,Code.DATA_VALUE_ERROR);
        }
        try {
            checkGroupDao.deleteAssociateCheckItemId(checkGroup.getId());
            checkGroupDao.updateCheckGroup(checkGroup);
            this.addCheckGroupAndCheckItem(checkGroup, checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(MessageConstant.EDIT_CHECKGROUP_FAIL,Code.UPDATE_ERROR);
        }
    }

    /**
     * 删除检查组
     *
     * @param id
     */
    @Transactional
    @Override
    public void deleteCheckGroup(Integer id) {
        long countWithItem = checkGroupDao.findCountByCheckGroupIdWithItem(id);
        long countWithSetmeal = checkGroupDao.findCountByCheckGroupIdWithSetmeal(id);
        if (countWithItem > 0 || countWithSetmeal > 0) {
            throw new BusinessException(MessageConstant.DATA_ASSOCIATE,Code.DELETE_ERROR);
        } else {
            checkGroupDao.deleteCheckGroup(id);
        }
    }

    /**
     * 查询所有检查组用于套餐项目展示
     *
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }


    /**
     * 添加关联的检查项
     *
     * @param checkGroup
     * @param checkitemIds
     */
    public void addCheckGroupAndCheckItem(CheckGroup checkGroup, Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                checkGroupDao.addCheckGroupAndCheckItem(checkGroup.getId(), checkitemId);
            }
        }
    }
}
